<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
       
    </style>
</head>
<body>
    <div id="app">
        <div>合计{{totalCount}}</div>
    </div>


    <script src="https://cdn.jsdelivr.net/npm/vue@2.7.16/dist/vue.js"></script>
    <script>
        const vue = new Vue({
            el: '#app',
            data: {
                username: '',
                age: 0,
                list: [
                    {id: 1, num: 1},
                    {id: 2, num: 9},
                    {id: 3, num: 2},
                    {id: 4, num: 4}
                ]
            },
            methods: {
               // 每次都会执行,实例方法
            },
            // 会缓存计算结果
            computed: {
                totalCount() {
                    console.log('计算属性执行了');
                    return this.list.reduce((sum, item) => sum + item.num, 0)
                },


                // 完整的计算属性写法 getter setter
                fullComputed : {
                    get() {
                        return this.list.reduce((sum, item) => sum + item.num, 0)
                    },
                    
                    set() {
                        // custom operation.
                    }
                }
            }
        })
    </script>
</body>
</html>